package net.jiastudy.biz.datasync.strategy.sdxm;

import net.jiastudy.biz.datasync.SyncUtil;
import net.jiastudy.biz.datasync.strategy.sdxm.bean.TeacherBean;
import net.jiastudy.biz.enums.DeptType;
import net.jiastudy.biz.enums.Gender;
import net.jiastudy.biz.sys.entity.Dept;
import net.jiastudy.biz.sys.service.DeptService;
import net.jiastudy.biz.user.entity.User;
import net.jiastudy.biz.user.service.UserService;
import net.jiastudy.core.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

@Component
public class AsyncTaskService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private UserService userService;

    @Resource
    private DeptService deptService;

    @Async("asyncPoolTaskExecutor")
    public Future<String> createTeachers(List<TeacherBean> beans) {
        List<Dept> depts = deptService.findAllDepts();
        Map<String, Dept> deptMap = new HashMap<>();
        for (Dept dept : depts) {
            deptMap.put(dept.getCode(), dept);
        }
        for (TeacherBean bean : beans) {
            try {
                User user = new User();
                user.setName(bean.getXM());
                user.setUsername(bean.getGH());
                user.setPassword(bean.getGH());
                if (bean.getXB().equals("男")) {
                    user.setGender(Gender.male);
                } else if (bean.getXB().equals("女")) {
                    user.setGender(Gender.female);
                }

                if (deptMap.containsKey(bean.getDWH())) {
                    user.setDeptId(deptMap.get(bean.getDWH()).getId());
                } else {
                    Dept dept = deptService.getDeptByCode(bean.getDWH());
                    if (null != dept) {
                        user.setDeptId(dept.getId());
                    } else {
                        Dept newDept = new Dept();
                        newDept.setName(bean.getDWMC());
                        newDept.setCode(bean.getDWH());
                        newDept.setType(DeptType.other);
                        newDept = deptService.createDept(newDept);
                        user.setDeptId(newDept.getId());
                    }
                }

                User dbUser = userService.getUserByUsername(user.getUsername());
                if (null == dbUser) {
                    userService.createUser(user);
                } else {
                    dbUser.setDeptId(user.getDeptId());
                    userService.updateUser(dbUser);
                    logger.info("姓名为：" + bean.getXM() + "工号为：" + bean.getGH() + "修改部门为" + bean.getDWH());
                }

            } catch(ServiceException e) {
                logger.error(e.getMessage() + "姓名为：" + bean.getXM() + "工号为：" + bean.getGH());
            }
        }

        SyncUtil.decrTeacherSyncBatchCount();
        return new AsyncResult<>("批量创建教师成功");
    }
}
